﻿package  
org.baicaix.demo.mouse {
	import org.baicaix.io.control.MouseUtil;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 * www.lite3.cn
	 * lite3@qq.com
	 * @author lite3
	 */
	[SWF(width="600", height="300", frameRate="30", backgroundColor="0xC7EDCC")]

	public class MouseDirection extends Sprite {

		private var prevX : Number = 0;
		private var prevY : Number = 0;
		private var prevAngle : Number = 0;
		private var title : TextField = new TextField();
		private var arrow : Sprite = new Sprite();
		private var btn : Sprite = new Sprite();
		private var btnTxt : TextField = new TextField();

		public function MouseDirection() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			initUI();
			
			//			this.contextMenu = MyContextMenu.getMyContextNenu();
			stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			
			btn.addEventListener(MouseEvent.CLICK, btnClickHandler);
			
			MouseUtil.init(stage);
			MouseUtil.hide();
		}

		private function btnClickHandler(e : MouseEvent) : void {
			if ("点击显示鼠标" == btnTxt.text) {
				MouseUtil.show();
				btnTxt.text = "点击隐藏鼠标";
			} else {
				MouseUtil.hide();
				btnTxt.text = "点击显示鼠标";
			}
		}

		private function mouseMoveHandler(e : MouseEvent) : void {
			arrow.x = e.stageX;
			arrow.y = e.stageY;
			arrow.rotation = MouseUtil.getAngle();
		}

		private function enterFrameHandler(e : Event) : void {
			if (MouseUtil.isMoving()) {
				title.text = "鼠标方向检测! 是否移动: true ";
			} else {
				title.text = "鼠标方向检测! 是否移动: false ";
			}
		}

		private function initUI() : void {
			// 标题
			title.text = "鼠标方向检测! 是否移动: true ";
			title.width = title.textWidth + 4;
			title.height = title.textHeight + 4;
			title.x = (stage.stageWidth - title.width) * .5;
			title.y = stage.stageHeight * .2;
			addChild(title);
			
			// 方向箭头
			arrow.graphics.lineStyle(3);
			arrow.graphics.moveTo(-50, 0);
			arrow.graphics.lineTo(50, 0);
			arrow.graphics.lineTo(0, -30);
			arrow.graphics.moveTo(50, 0);
			arrow.graphics.lineTo(0, 30);
			arrow.mouseChildren = false;
			arrow.mouseEnabled = false;
			addChild(arrow);
			
			// 按钮
			btnTxt.selectable = false;
			btnTxt.mouseEnabled = false;
			btnTxt.text = "点击显示鼠标";
			btnTxt.width = btnTxt.textWidth + 4;
			btnTxt.height = btnTxt.textHeight + 4;
			btn.addChild(btnTxt);
			btn.buttonMode = true;
			btn.graphics.beginFill(0xFFFF00);
			btn.graphics.drawRoundRect(0, 0, 80, 20, 15);
			addChild(btn);
		}
	}
}